<?php
	ob_start();
	session_start();

	function __autoload($class_name) {
		include './'.$class_name . '.php';
	}

	$dbConfig = new DBConfigNew(); 
	$conn = $dbConfig->getConnection();
	$mysqli=$conn;
	$result = null;
	$orderdetails = null;

	$query = "CALL hambaaco_merchandize.index_info_q();";
	$conn->multi_query($query);

	$productId = null;
	$productName = null;
	$productPrice = null;
	$productDiscount = null;
	$productDiscountPrice = null;
	$attributeName = null;
	$attributeValues = array();
	$attributes = array();
	$attributesArray = array();
	$productsArray = array();
	$results = array();
	$isResults = false;
	
		
					
		if ($result = $conn->store_result()) {
			while($row = $result->fetch_array()){
				$isResults = true;
				if (is_null($productId)){
				
					$productId =$row['id_product'];	
					$productName = $row['tx_nm_product'];
					$productPrice = $row['tx_price'];
					$productDiscount = $row['tx_discount'];
					$productDiscountPrice = floor((100 - $row['tx_discount'])*$row['tx_price']/100);
					
				} else if ($productId != $row['id_product']){
				
					$attribute = array();
					$attribute['attributeName'] = $attributeName;
					$attribute['value'] = $attributeValues;
					array_push($attributesArray,$attribute);
					$attributeValues = array();
					$attributeName = $row['tx_nm_attribute'];
					$products = array();
					$products['productId'] = $productId;
					$products['productName'] = $productName;
					$products['productPrice'] = $productPrice;
					$products['productDiscount'] = $productDiscount;
					$products['discountPrice'] = $productDiscountPrice;
					$products['productAttrs'] = $attributesArray;
					array_push($productsArray,$products);
					$attributesArray = array();
					$productId = $row['id_product'];	
					$productName = $row['tx_nm_product'];
					$productPrice = $row['tx_price'];
					$productDiscount = $row['tx_discount'];
					$productDiscountPrice = floor((100 - $row['tx_discount'])*$row['tx_price']/100);
					
				}				

				if (is_null($attributeName)){
				
					$attributeName =$row['tx_nm_attribute'];
				
				}else if( $attributeName != $row['tx_nm_attribute']){					
				
					$attribute = array();
					$attribute['attributeName'] = $attributeName;
					$attribute['value'] = $attributeValues;
					array_push($attributesArray,$attribute);
					$attributeValues = array();
					$attributeName = $row['tx_nm_attribute'];
				
				}	
				
				array_push($attributeValues,$row['tx_value']);	
			}
			
			if($isResults) {
			
				$attribute = array();
				$attribute['attributeName'] = $attributeName;
				$attribute['value'] = $attributeValues;
				array_push($attributesArray,$attribute);
				$attributeValues = array();
				$attributeName = $row['tx_nm_attribute'];
				$products = array();
				$products['productId'] = $productId;
				$products['productName'] = $productName;
				$products['productPrice'] = $productPrice;
				$products['productDiscount'] = $productDiscount;
				$products['discountPrice'] = $productDiscountPrice;
				$products['productAttrs'] = $attributesArray;
				array_push($productsArray,$products);
			
			}			
			
			$result->close();
			$results['#success'] = "true";
			$results['productInfo'] = $productsArray;
		} else	{
			$results['#success'] = "false";
		}			
				

	$dbConfig->closeConnection();
	echo json_encode($results);
	ob_end_flush();
?>